<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 包裹列表内容 -->
    <el-card class="box-card">
      <el-button type="primary" @click="addClassify">添加分类</el-button>
    </el-card>
    <!-- 第三方插件 vue-table-with-tree-grid -->
    <zk-table
      ref="table"
      sum-text="sum"
      :data="categoriesData"
      :columns="columns"
      :border="true"
      :show-header="true"
      :show-row-hover="true"
      :show-index="true"
      :tree-type="true"
      :is-fold="true"
      :expand-type="false"
      :selection-type="false"
    >
      <el-table-column type="index"></el-table-column>
      <!-- 根据 cat_level 显示不同图标 -->
      <template slot="cat_level" scope="scope">
        <i class="el-icon-success" style="color: lightgreen" v-if="scope.row.cat_level == '0'"></i>
        <i class="el-icon-error" style="color:red" v-else></i>
      </template>
      <!-- 显示不同等级 -->
      <template slot="cat_pid" scope="scope">
        <el-tag type="success" v-if="scope.row.cat_pid == '0'">一级</el-tag>
        <el-tag type="warning" v-else-if="scope.row.cat_pid == '1'">二级</el-tag>
        <el-tag type="warning" v-else>三级</el-tag>
      </template>
      <template slot="operate" scope="scope">
        <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          icon="el-icon-delete"
          @click="deleteCategoriesId(scope.row.cat_id)"
        >删除</el-button>
      </template>
    </zk-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 点击添加分类按钮弹出对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCategoriesVisible"
      width="70%"
      @close="addDialogClosed"
    >
      <!-- 需要修改的内容 -->
      <el-form :model="addCategories" ref="editUserRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCategories.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <div class="block">
            <el-cascader
              v-model="value"
              :props="{value:'cat_id',label:'cat_name'}"
              :options="addClassifyList"
              clearable
              @change="handleChange"
            ></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCategoriesVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAddCategories">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
           
<script>
import { categoriesList, addCategoriesList, deleteCategories } from "@/api";
export default {
  data() {
    return {
      //分页参数
      pagenum: 1,
      //每页显示条数
      pagesize: 4,
      //总条数
      total: 0,
      // 获取到的用户列表存储位置
      categoriesData: [],
      //显示在页面需要的规则
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          prop: "cat_level",
          type: "template",
          template: "cat_level"
        },
        {
          label: "排序",
          prop: "cat_pid",
          type: "template",
          template: "cat_pid"
        },
        {
          label: "操作",
          type: "template",
          template: "operate"
        }
      ],
      addCategoriesVisible: false,
      //添加用户需要传给后台的参数
      addCategories: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0
      },
      //点击添加获取到的商品分类列表
      addClassifyList: {},
      // 选中的父级分类的Id数组
      value: []
    };
  },
  methods: {
    //获取商品列表信息
    async getCategoriesList() {
      var { data: res } = await categoriesList({
        type: [3],
        pagesize: this.pagesize,
        pagenum: this.pagenum
      });
      this.categoriesData = res.data.result;
      // console.log(res.data.result, 99);
      this.total = res.data.total;
    },
    //分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pagesize = val;
      this.getCategoriesList();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pagenum = val;
      this.getCategoriesList();
    },
    // 关闭分类清空内容
    addDialogClosed() {
      this.$refs.editUserRef.resetFields();
      this.value = [];
      this.addCategories.cat_pid = 0;
      this.addCategories.cat_level = 0;
    },
    //获取选中
    handleChange(value) {
      // console.log(value,9898);
      if (value.length > 0) {
        this.addCategories.cat_pid = value[value.length - 1];
        this.addCategories.cat_level = value.length;
      } else {
        this.addCategories.cat_pid = 0;
        this.addCategories.cat_level = 0;
      }
      console.log(this.addCategories);
    },
    //获取全部分类列表信息
    async addClassify() {
      var { data: res } = await categoriesList({ type: [3] });
      console.log(res.data, 99);
      this.addClassifyList = res.data;
      this.addCategoriesVisible = true;
    },
    //点击添加分类中确认按钮触发函数
    async confirmAddCategories() {
      var { data: res } = await addCategoriesList(this.addCategories);
      // console.log(res, 1212);
      if (res.meta.status !== 201) this.$message.error("添加失败！");
      this.$message.success("添加成功！");
      this.getCategoriesList();
      this.addCategoriesVisible = false;
    },
    async deleteCategoriesId(id) {
      var state = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
      // console.log(aa);
      if (state == "confirm") {
        var { data: res } = await deleteCategories(id);
        console.log(res);
        if (res.meta.status !== 200) return this.$message.error("删除失败");
        this.$message.success("删除成功");
        this.getCategoriesList();
      }
    }
  },
  created() {
    this.getCategoriesList();
  }
};
</script>
<style scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
</style>